Chapter 1: JavaScript for Beginners
Before you can begin learning how to code, there are some Prerequisites. You have to at least have a basic knowledge of HTML and CSS.
HTML stands for Hypertext Markup language. It is not considered a programming language. Like its name suggests, it is a Markup language that is used when structuring a web page. At its core, it is a very simple language that incorporates the elements applied to text to give it a distinct meaning. You can use these elements to wrap, markup or enclose components on your page and make them look or act a particular way. It can determine whether the text in your document comes in the form of a paragraph, has bullet points or comes in the form of a table. It can also embed pictures and videos onto your page.
When you go on to any website, a number of files are dished out to you. At a rudimentary level your website will present you with an HTML page and a script.js page. The HTML page is the bare bones of the web page. It tells the browser what to show and where everything goes. So if there needs to be a title at the top, a paragraph in the middle and a picture at the bottom, the HTML file is responsible for relaying that information to the browser. This is all well and good but it’s very limiting because the page will always be static and the data cannot change. That’s where JavaScript comes in. JavaScript is able to manipulate the appearance of the website by accessing the DOM. DOM stands for Document Object Model. It tells JavaScript what is on the page and then allows it to make changes to elements of the page. DOM makes it easier to read, access and obtain the content of an HTML document. If we break it down even further, the Document is the HTML file, the object is the tags and elements within the HTML file and the Model is the Layout or structure. Without DOM, JavaScript would not have a perception of any of the elements on a web page and would therefore not be able to change or manipulate them. The DOM information is available through an API. API stands for Application Programming Interface. It basically gives JavaScript an opening to control things on the HTML page. The API acts as the go between for JavaScript and HTML.
HTML syntax is simply the way HTML is written. HTML elements are structured a certain way; they are made up of the opening tag, the content and the closing tag. The opening tag is where the element commences. Visually, it is the name of the element wrapped in opening and closing angle brackets. For example, when your element is a paragraph, the opening tag would look like this: <p>. The content of the element goes in the middle, in this case, it would be the text in the paragraph. The closing tag marks where the element ends. It is similar to the opening tag, but it has a forward slash after the first bracket. In this case it would look like this: </p>. If you forget to insert the forward slash or fail to include the closing tag altogether, you may not achieve the desired results.
Elements are split into two categories, namely: Block level and inline elements. Block-level elements create blocks that you can actually see on a page, they take up the full width of a web page by creating a line break. Inline elements take up only as much as they need and are able to add content to a page without any line breaks.
CSS or Cascading Style Sheets is a styling language used for describing the appearance of a document written in HTML. It is used in the formatting and layout of web pages. It is basically responsible for making your web page look really good. CSS syntax is pretty straightforward. The first part of the syntax is the selector, it is followed by opening and closing curly brackets. The brackets are used to show the start and the end of the style that is going to be applied to the selector. It can look something like this: selector { property1: value;}. Property refers to color, size or length, while value refers to the text or image the property is being applied to.
Syntax
In order to execute JavaScript, we have to put statements inside of script HTML tags on a web page. For example: <script> JavaScript code </script>. The script tags are made up of two properties:
➔ Language
Defines the scripting language you are working with. The value will always be JavaScript.
➔ Type
Stipulates the scripting language being used and should always be set to ‘text/javascript’.
Your segment will look like this:
<script language = “javascript” type = “text/javascript”> JavaScript code </script> (JavaScript - Syntax, n.d.)
You have complete freedom to use spaces, tabs and newlines in your program, you can also format and indent your code in a way that is simple to read and understand for you, but JavaScript ignores these things. In JavaScript, simple statements are usually followed by a semicolon, but you have the choice to omit it if your statements are written on different lines.
If your statement looks like this:
<script language = “javascript” type = “text/javascript”>
<! - -
var1=20
var2=30
// -–>
</script> (JavaScript - Syntax, n.d.)
You can write it without semicolons. When your statement is written in a single line format and looks like this:
<script language = “javascript” type = “text/javascript”>
<! --
var1=20; var2=30;
// -–>
</script> (JavaScript - Syntax, n.d.)
Then you must make use of semicolons.
JavaScript is case sensitive, all variables and other identifiers must be written in all caps.
Comments in JavaScript are used to add notes to your code, or deactivate portions of the code without fully removing them. We produce comments by putting a forward slash before a single line or adding a forward slash followed by an asterisk after several lines. Commenting on code is a core feature of a lot of programming languages.
Here is an example of how to use comments in JavaScript:
<script language = “javascript” type = “text/javascript”>
<! --
//comment
/*
*multi-line comment
*multi-line comment line 2
*/
//-- >
</script> (JavaScript - Syntax, n.d.)
When a user operates a web page, JavaScript and HTML interact, this is called an Event. That means that any and all actions performed on a web page are an event. Developers code responses to all events. So if you scroll over any of the windows on your browser, and you see the name of the website and web address, that is a response to an event. There are many event types, two of them being:
Onload - The Onload event gets activated when the page loads
Onmouseover - Is activated when the cursor moves on or around an element
JavaScript in Browsers
JavaScript is supported on all current browsers, but sometimes you may have to manually enable or disable it. This is how you manage it on each different browser:
Firefox:
● Open a new tab, select the address bar and then type in: ‘about: config’
● The warning page will appear, accept the risk and continue
● The list of configure options will appear in the browser
● Go to the search bar and type in javascript.enabled
● The toggle will appear and you will be able to switch between disabled and enabled as you please.
Chrome:
● Click on the menu in the top right hand corner
● Click on settings
● Go down to the end of the page and click on Show advanced settings
● Go to the privacy section and click on the content settings button
● There you will find the JavaScript section, you will have the option to either allow or disallow sites to run JavaScript.
Edge:
● Click the settings and more button
● Select settings
● Find the JavaScript section
● Choose whether to enable or disable JavaScript with the toggle
How to Get Started Writing JavaScript
There are three methods of writing JavaScript. The first, is to write it directly into the Browser. This is the easiest way because you don’t need to set anything up and you can get started by trying out some simple comments. The downside is that if you refresh the page, the code will disappear and you will have to start from scratch. You can also only execute one command at a time.
If you are developing, you are going to need to be able to save your code and execute multiple commands at any given time. For that you are going to want to write JavaScript in a simple text file and then send it to the browser to execute. Browsers accept files with the extension .html. You can create HTML files with Notepad in windows and with Textedit on Mac. Once you have written your code into the file, you need to change the format to HTML. You can achieve this by selecting the html extension when saving.
Even though you can write multiple lines of code and include various comments and variables, there are some disadvantages to using a simple text editor. You have to be extremely meticulous because they don’t help you in writing code. Keywords are not highlighted, syntax errors cannot be detected and code completion is completely up to you.
Another way you can write your code is with Special Code Editors. Special Code Editors support various programming languages and have specialized features to help you write your code. They highlight different keywords and alert you when there is a syntax error. An editor that is available for free online is Visual Studio Code.
When you are coding, you can put your code anywhere on the file, but the most popular way to do it is either in the head or the body. We put script in the head section if we want it to run when a user clicks on something. It will look something like this:
<html>
<head>
<script type = “text/javascript”>
< ! - -
function sayHello() {
alert(“Hi there testing”)
}
//- - >
</script>
</head>
<body>
<input type = “button” onclick = “sayHello()” value = “Say Hello”
</body>
<html> (JavaScript - Syntax, n.d.)
That code would produce a button, and once you click on that button there would be a pop up that says Hi there testing.
Datatypes in JavaScript
Data Types are the values that are supported by a programming language. In JavaScript you will work with six; Numbers, BigInt, Strings, Null, Undefined and Booleans.
Numbers
Before we can understand the numbers value, we need to have an idea of what ECMAscript is. ECMA is a firm that sets standards for technology. ECMAScript is one of those standards. ECMAScript carries specifications for scripting languages, numbers and BigInt are built into it. JavaScript keeps to the ECMAScript specification. This means that when we are actually coding we make use of JavaScript and not ECMAScript. The number type is used to represent integer values and floating point values. Integers do not have decimal points, and floating numbers do.
BigInt
BigInt is an abbreviation for Big Integer. An integer is a whole number without decimals. This data type was created for JavaScript in early 2020 because the number data type was limited. When the script encountered numbers higher than 2^53 - 1 they tended to lack accuracy thus affecting the efficacy of applications.
Strings
A string is a chain of one or more symbols. It could include anything from letters to numbers. Strings are primitive which means they cannot change.
Null
Null is the value shown when there is no object.
Undefined
A variable that can’t be identified by its characteristics becomes undefined in JavaScript. A lot of people are unsure what the difference between Null and Undefined is. Undefined is a variable that has no value, while null is an empty value assigned to a variable.
Booleans
Booleans only have two possible values. True or false. Boolean is usually used with conditional statements in JavaScript.
Once you have a firm grasp on the Syntax and all the jargon, you’ll want to turn your attention to programming, because it’s the fun part, but it is also going to determine how your apps and websites turn out.